package com.pk.android.view;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBar.LayoutParams;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

import com.pk.android.R;
import com.pk.android.dummy.temp_source;
import com.pk.android.model.BaseFragment;
import com.pk.android.model.League;
import com.pk.android.view.adapter.ExpandListAdapter;

public class DashboardPK_Fragment extends BaseFragment {
	
	public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
	OnMyLeagueClickListener mCallback;
	ExpandableListAdapter ExpAdapter;
	View view;
	DisplayMetrics metrics;
	int width;
	ExpandableListView expList;
	
	public interface OnMyLeagueClickListener {
        public void onMyLeaguesClick(int position);
    }
	
	public static final DashboardPK_Fragment newInstance(String message)
	{
		DashboardPK_Fragment f = new DashboardPK_Fragment();
		Bundle bdl = new Bundle(1);
	   bdl.putString(EXTRA_MESSAGE, message);
	   f.setArguments(bdl);

	   f.setTitle(message);
	   
		return f;
	}

	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnMyLeagueClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnMyLeagueClickListener");
        }
    }
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    	// Inflate the layout for this fragment   
    	//String message = getArguments().getString(EXTRA_MESSAGE);
    	
    	metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        
    	view = inflater.inflate(R.layout.fragment_pk_dashboard, container, false);
        return view;
    }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
		super.onCreate(savedInstanceState);
		
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		final List<League> myLeaguesList = temp_source.getMyLeagueList();
		ViewGroup layout = (ViewGroup) getActivity().findViewById(R.id.leagues_layout);
		ExpandableListView expandListView = new ExpandableListView(getActivity());
		expandListView.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT));
		
		ExpAdapter = new ExpandListAdapter(getActivity(), myLeaguesList);
		expandListView.setAdapter(ExpAdapter);
		//expandListView.setGroupIndicator(this.getResources().getDrawable(R.drawable.default_group_indicator));//or add an icon @drawable/settings_selector
		expandListView.setGroupIndicator(null);//or add an icon @drawable/settings_selector
	    expandListView.setIndicatorBounds(width - GetDipsFromPixel(85), width - GetDipsFromPixel(35));
	       
		int count = ExpAdapter.getGroupCount();
		for (int position = 1; position <= count; position++)
			expandListView.expandGroup(position - 1);
		
		expandListView.setOnGroupCollapseListener(new OnGroupCollapseListener(){

			@Override
			public void onGroupCollapse(int groupPosition) {
				// TODO Auto-generated method stub
				//String s = "";
			}
			
		});
		
		expandListView.setOnGroupExpandListener(new OnGroupExpandListener(){

			@Override
			public void onGroupExpand(int arg0) {
				// TODO Auto-generated method stub
				//String s = "";
			}
			
		});
		expandListView.setOnGroupClickListener(new OnGroupClickListener(){
			
			boolean result = false;
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					final int groupPosition, long id) {
				
				v.findViewById(R.id.img_view_league).setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						mCallback.onMyLeaguesClick(groupPosition);
					}
				});
				

				v.findViewById(R.id.img_list_collapse).setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						result =  false;
					}
				});
				
				return result;
			}});
		
		layout.addView(expandListView);
		
	}
	
	public int GetDipsFromPixel(float pixels)
    {
     // Get the screen's density scale
     final float scale = getResources().getDisplayMetrics().density;
     // Convert the dps to pixels, based on density scale
     return (int) (pixels * scale - 0.5f);
}
}
